package com.anolesoft.twelvePlan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.twelvePlan.dto.MesWXInfoDTO;
import com.anolesoft.twelvePlan.entity.MesWxInfoDO;
import com.anolesoft.twelvePlan.mapper.MesGyInfoMapper;
import com.anolesoft.twelvePlan.mapper.MesWXInfoMapper;
import com.anolesoft.twelvePlan.service.MesWXInfoService;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zyq
 * @date 2022/2/23
 */
@Service
@Slf4j
public class MesWXInfoServiceImpl implements MesWXInfoService {
    @Autowired
    private MesWXInfoMapper mesWXInfoMapper;
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Override
    public PageData<List<MesWXInfoDTO>> queryWXInfo(MesWXInfoDTO mesWXInfoDTO, Integer pageNo, Integer pageSize) {
        Page<MesWxInfoDO> page = new Page<>(pageNo, pageSize);
        List<MesWxInfoDO> MesWxInfoDOS = this.mesWXInfoMapper.queryWXInfo(MesWxInfoDO.builder()
                .jx(CommonUtils.stringNull(mesWXInfoDTO.getJx()))
                .partid(CommonUtils.stringNull(mesWXInfoDTO.getPartid()))
                .category(CommonUtils.stringNull(mesWXInfoDTO.getCategory()))
                .neworcontinue(CommonUtils.stringNull(mesWXInfoDTO.getNeworcontinue()))
                .factoryId(CommonUtils.stringNull(mesWXInfoDTO.getFactoryId()))
                .build(), page);
        List<MesWXInfoDTO> mesWXInfoDTOS = MesWxInfoDOS.stream()
                .map(t -> BeanUtil.copyProperties(t, MesWXInfoDTO.class))
                .collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), mesWXInfoDTOS);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertWXInfo(MesWXInfoDTO mesWXInfoDTO) {
        int repeat = mesWXInfoMapper.repeatWXInfo(MesWxInfoDO.builder()
                .jx(CommonUtils.stringNull(mesWXInfoDTO.getJx()))
                .partid(CommonUtils.stringNull(mesWXInfoDTO.getPartid()))
                .pgh(CommonUtils.stringNull(mesWXInfoDTO.getPgh()))
                .build());
        log.debug("repeat:{}" + repeat);
        int count = 0;
        if (repeat == 0) {
            count = this.mesWXInfoMapper.insertWXInfo(MesWxInfoDO.builder()
                    .jx(CommonUtils.stringNull(mesWXInfoDTO.getJx()))
                    .partid(CommonUtils.stringNull(mesWXInfoDTO.getPartid()))
                    .name(CommonUtils.stringNull(mesWXInfoDTO.getName()))
                    .material(CommonUtils.stringNull(mesWXInfoDTO.getMaterial()))
                    .ljsize(CommonUtils.stringNull(mesWXInfoDTO.getLjsize()))
                    .pgh(CommonUtils.stringNull(mesWXInfoDTO.getPgh()))
                    .djnum(mesWXInfoDTO.getDjnum())
                    .cgnum(mesWXInfoDTO.getCgnum())
                    .wxprocess(CommonUtils.stringNull(mesWXInfoDTO.getWxprocess()))
                    .neworcontinue(CommonUtils.stringNull(mesWXInfoDTO.getNeworcontinue()))
                    .selfflag(CommonUtils.stringNull(mesWXInfoDTO.getSelfflag()))
                    .category(CommonUtils.stringNull(mesWXInfoDTO.getCategory()))
                    .wxfactory(CommonUtils.stringNull(mesWXInfoDTO.getWxfactory()))
                    .remarks1(CommonUtils.stringNull(mesWXInfoDTO.getRemarks1()))
                    .remarks(CommonUtils.stringNull(mesWXInfoDTO.getRemarks()))
                    .createuid(CommonUtils.stringNull(mesWXInfoDTO.getCreateuid()))
                    .factoryId(CommonUtils.stringNull(mesWXInfoDTO.getFactoryId()))
                    .build());
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateWXInfo(MesWXInfoDTO mesWXInfoDTO){
        int repeat = mesWXInfoMapper.repeatWXInfo(MesWxInfoDO.builder()
                .jx(CommonUtils.stringNull(mesWXInfoDTO.getJx()))
                .partid(CommonUtils.stringNull(mesWXInfoDTO.getPartid()))
                .pgh(CommonUtils.stringNull(mesWXInfoDTO.getPgh()))
                .objuid(CommonUtils.stringNull(mesWXInfoDTO.getObjuid()))
                .build());
        int count = 0;
        if (repeat == 0) {
            count = this.mesWXInfoMapper.updateWXInfo(MesWxInfoDO.builder()
                    .objuid(CommonUtils.stringNull(mesWXInfoDTO.getObjuid()))
                    .jx(CommonUtils.stringNull(mesWXInfoDTO.getJx()))
                    .partid(CommonUtils.stringNull(mesWXInfoDTO.getPartid()))
                    .name(CommonUtils.stringNull(mesWXInfoDTO.getName()))
                    .material(CommonUtils.stringNull(mesWXInfoDTO.getMaterial()))
                    .ljsize(CommonUtils.stringNull(mesWXInfoDTO.getLjsize()))
                    .pgh(CommonUtils.stringNull(mesWXInfoDTO.getPgh()))
                    .djnum(mesWXInfoDTO.getDjnum())
                    .cgnum(mesWXInfoDTO.getCgnum())
                    .wxprocess(CommonUtils.stringNull(mesWXInfoDTO.getWxprocess()))
                    .neworcontinue(CommonUtils.stringNull(mesWXInfoDTO.getNeworcontinue()))
                    .selfflag(CommonUtils.stringNull(mesWXInfoDTO.getSelfflag()))
                    .category(CommonUtils.stringNull(mesWXInfoDTO.getCategory()))
                    .wxfactory(CommonUtils.stringNull(mesWXInfoDTO.getWxfactory()))
                    .remarks1(CommonUtils.stringNull(mesWXInfoDTO.getRemarks1()))
                    .remarks(CommonUtils.stringNull(mesWXInfoDTO.getRemarks()))
                    .modifyuid(CommonUtils.stringNull(mesWXInfoDTO.getModifyuid()))
                    .build());
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteWXInfo(MesWXInfoDTO mesWXInfoDTO) {
        int count = this.mesWXInfoMapper.deleteWXInfo(MesWxInfoDO.builder()
                .objuid(CommonUtils.stringNull(mesWXInfoDTO.getObjuid()))
                .deleteuid(CommonUtils.stringNull(mesWXInfoDTO.getDeleteuid()))
                .build());
        return count;
    }

    @Override
    public int exportWXInfo(MesWXInfoDTO mesWXInfoDTO) {
        int count = this.mesWXInfoMapper.exportWXInfo(MesWxInfoDO.builder()
                .jx(CommonUtils.stringNull(mesWXInfoDTO.getJx()))
                .partid(CommonUtils.stringNull(mesWXInfoDTO.getPartid()))
                .category(CommonUtils.stringNull(mesWXInfoDTO.getCategory()))
                .neworcontinue(CommonUtils.stringNull(mesWXInfoDTO.getNeworcontinue()))
                .factoryId(CommonUtils.stringNull(mesWXInfoDTO.getFactoryId()))
                .build());
        return count;
    }

    @Override
    public void uploadWXInfo(List<MesWxInfoDO> list) {
        SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
        MesWXInfoMapper mesWXInfoMapper = sqlSession.getMapper(MesWXInfoMapper.class);
        list.forEach(a -> {
            mesWXInfoMapper.insert(a);
        });
        sqlSession.commit();
    }

    @Override
    public int deleteSelect(MesWXInfoDTO mesWXInfoDTO) {
        int count = this.mesWXInfoMapper.deleteSelect(MesWxInfoDO.builder()
                .jx(CommonUtils.stringNull(mesWXInfoDTO.getJx()))
                .partid(CommonUtils.stringNull(mesWXInfoDTO.getPartid()))
                .category(CommonUtils.stringNull(mesWXInfoDTO.getCategory()))
                .neworcontinue(CommonUtils.stringNull(mesWXInfoDTO.getNeworcontinue()))
                .factoryId(CommonUtils.stringNull(mesWXInfoDTO.getFactoryId()))
                .deleteuid(CommonUtils.stringNull(mesWXInfoDTO.getDeleteuid()))
                .build());
        return count;
    }

}

